package com.android.pagemarket.ui.activity.main;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.android.pagemarket.R;
import com.android.pagemarket.io.protocol.in.HttpAction;
import com.android.pagemarket.io.protocol.in.HttpAction.ActionListener;
import com.android.pagemarket.io.protocol.in.RegisterAction;
import com.android.pagemarket.io.protocol.out.HttpResult;
import com.android.pagemarket.ui.activity.base.TemplateActivity;
import com.android.pagemarket.util.Util;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-10-30
 * @see : 注册账号
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class RegisterActivity extends TemplateActivity implements OnClickListener
{

    private EditText tx_name;
    private EditText tx_phone;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_register);
        initViews();
    }
    
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_register:
            {
                doRegister();
                break;
            }
        }
    }
    
    private void initViews()
    {
        tx_name = getViewById(R.id.tx_name);
        tx_phone = getViewById(R.id.tx_phone);
    }

    private void doRegister()
    {
        String name = tx_name.getEditableText().toString().trim();
        String phone = tx_phone.getEditableText().toString().trim();
        
        if(name.length() == 0 || !Util.isValidName(name))
        {
            showToast(getString(R.string.tip_name_check));
            return;
        }
        if(phone.length() == 0 || !Util.isPhoneNumberValid(phone))
        {
            showToast(getString(R.string.tip_phone_check));
            return;
        }
        
        RegisterAction action = new RegisterAction(name, phone);
        action.setActionListener(new ActionListener<HttpResult>()
        {
            @Override
            public void onSucceed(HttpResult result)
            {
//                showLoading(false);
                hideLoding();
                if(result.isNoError())
                {
                    showDialog(null, getString(R.string.tip_regist_ok), R.string.tx_OK, 
                            new  DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            finish();
                        }
                    });
                }else
                {
                    showToast(result.resultInfo);
                }
            }
            @Override
            public void onError(int resultCode)
            {
//                showLoading(false);
                hideLoding();
                showToast(HttpAction.getErrorMean(resultCode));
            }
        });
//        showLoading(true);
        showLoding(null, "注册中...");
        action.submit();
    }
    
    public static void startActivity(Context context)
    {
        Intent it = new Intent(context, RegisterActivity.class);
        
        context.startActivity(it);
    }
}
